package com.hehe.auth.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.OAuth2RequestFactory;
import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationManager;
import org.springframework.security.oauth2.provider.client.ClientDetailsUserDetailsService;
import org.springframework.security.oauth2.provider.request.DefaultOAuth2RequestFactory;
import org.springframework.security.oauth2.provider.token.DefaultTokenServices;
import org.springframework.security.oauth2.provider.token.ResourceServerTokenServices;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.InMemoryTokenStore;

@Configuration
@EnableAuthorizationServer
public class AuthorizationConfig extends AuthorizationServerConfigurerAdapter{
	@Autowired
	private TokenGranterDelegater tokenGranter;
	@Autowired
	private ClientDetailsService clientDetailsService;
	@Override
	public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
		security.tokenKeyAccess("permitAll()").checkTokenAccess(
				"isAuthenticated()");
	}

	@Override
	public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
		clients.inMemory()
		.withClient("acme")
		.secret("acmesecret")
		.authorizedGrantTypes("authorization_code", "refresh_token",
				"password","DEVICE","SMS","WECHAT").scopes("openid");
	}

	@Override
	public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
		endpoints.authenticationManager(authenticationManager());
		endpoints.tokenGranter(tokenGranter);
		endpoints.userDetailsService(userDetailsService());
	}
	
	@Bean
	public AuthenticationManager authenticationManager() {
		OAuth2AuthenticationManager authenticationManager = new OAuth2AuthenticationManager();
		authenticationManager.setTokenServices(tokenServices());
		authenticationManager.setClientDetailsService(clientDetailsService);
		return authenticationManager;
	}
	@Bean
	public UserDetailsService userDetailsService() {
		return new ClientDetailsUserDetailsService(clientDetailsService);
	}
	@Bean
	public ResourceServerTokenServices tokenServices() {
		DefaultTokenServices tokenServices = new DefaultTokenServices();
		TokenStore tokenStore = new InMemoryTokenStore(); 
		tokenServices.setTokenStore(tokenStore);
		return tokenServices; 
	}
	
	@Bean
	public OAuth2RequestFactory oAuth2RequestFactory(){
		DefaultOAuth2RequestFactory oAuth2RequestFactory = new DefaultOAuth2RequestFactory(clientDetailsService);
		return oAuth2RequestFactory;
	}
}
